<template>
  <el-form :model="form" :rules="rules" ref="ruleFormRef" status-icon :label-position="labelPosition" label-width="120px"
    :inline="true" class="demo-form-inline">
    <el-form-item label="姓名" prop="userName"  maxlength="20" show-word-limit >
      <!-- <SelectOption :selectValue="form.userName" :options="data.userAllList" @change-value="changeValue"> </SelectOption> -->
      <user-list-modal
          v-model="form.userName"
          @update:modelValue="changeValue"
          :default-users="[]"
          :multiple="false"
          :devType="'platform'"
        />
    </el-form-item>

    <el-form-item label="所属单位" prop="unitName">
      <popup-tree-input :data="orgAllList" :props="data.popupTreeProps" :prop="deptForm.parentName == null
          ? $t('message.org.top_menu')
          : form.unitName
        " :node-key="'' + deptForm.parentId" :current-change-handle="changeValue1" />

    </el-form-item>

    <el-form-item label="人才级别" prop="talentLevel">
      <SelectOption :selectValue="form.talentLevel" :options="data.levelOptions" @change-value="changeValue2"> </SelectOption>
    </el-form-item>

    <el-form-item label="人才类别" prop="talentClass">
      <SelectOption :selectValue="form.talentClass" :options="data.typeOptions" @change-value="changeValue3">
      </SelectOption>
    </el-form-item>

    <el-form-item label="获得称号" prop="title">
      <SelectOption :selectValue="form.title" :options="data.titleList" @change-value="changeValue4"> </SelectOption>
    </el-form-item>

    <el-form-item label="认定时间" prop="confirmDate">
      <el-date-picker v-model="form.confirmDate" format="YYYY-MM-DD" type="date" placeholder="选择日期" clearable
        @change="handleChangeDate" />
    </el-form-item>

    <el-form-item label="认定单位" prop="awardUnit">
      <el-input maxlength="50" show-word-limit  v-model="form.awardUnit" />
    </el-form-item>

    <el-form-item label="备注" style="width: 100%">
      <el-input v-model="form.remark" :autosize="{ minRows: 2 }" maxlength="300"  show-word-limit  type="textarea" placeholder="请输入" />
    </el-form-item>
  </el-form>
</template>
<script lang="ts">
import UserListModal from '@/views/project/components/UserListModal/index.vue';
import PopupTreeInput from '@/views/science/components/PopupTreeInput/index.vue';
import SelectOption from '@/views/science/components/selectOption/index.vue';
import { computed, reactive, ref, watch } from 'vue';
import { useStore } from 'vuex';
export default {
  components: {
    SelectOption,PopupTreeInput,UserListModal
  },
  props: {
    orgAllList: {
            type: Array,
            required: true,
        }
  },
  emits: {
    valueFlag: null,
  },
  setup(props, { emit }) {
    const store = useStore()
    const labelPosition = ref('right')
    const deptForm = {
      parentId: 0,
      parentName: '',
    }

    const data = reactive({

      popupTreeProps: {
        label: 'name',
        children: 'children',
      },
      activeName: ref('first'),
      addUnit: false,
      innerValue: '',
      outsideValue: '',

      levelOptions: computed(() => store.state.innovativeTalents.kjlevelSelect),
      typeOptions: [],

      // titleList: computed(() => store.state.innovativeTalents.titleList),
      titleList: [],

      userAllList: computed(() => store.state.technologyStrength.userAllList),


      sexList: computed(() => store.state.innovativeTalents.sexList),
    })

    const form = reactive(store.state.innovativeTalents.tleadershipInfo)

    if(form.talentLevelId){
      data.levelOptions.map((item)=>{
        if(item.itemValue == form.talentLevelId){
          data.typeOptions = item.children
        }
      })
    }


    watch(form, (newValue, oldValue) => {
      console.log(form)
      store.commit('innovativeTalents/gettleadershipInfo', { ...form })
    });

    const changeValue = (item) => {
      form.userName = item.cname
      form.userId = item.userId
    }
    const changeValue1 = (item) => {
      form.unitName = item.name
      form.unitId = item.id
    }
    const changeValue2 = (item) => {
      form.talentLevel = item.itemText
      form.talentLevelId = item.itemValue
      data.typeOptions = item.children
      form.talentClass = ''
      form.talentClassId = ''
      form.title = '',
      form.titleId =  ''
    }
    const changeValue3 = (item) => {
      form.talentClass = item.itemText
      form.talentClassId = item.itemValue

      if (item.children != null) {
        data.titleList = item.children
        form.title = '',
        form.titleId =  ''
      }
    }
    const changeValue4 = (item) => {
      form.title = item.itemText
      form.titleId = item.itemValue
    }
    const changeValue5 = (item) => {
      form.sex = item.itemText
    }

    const rules = reactive({
      userName: [
        { required: true, message: '请选择姓名', trigger: 'change' },
      ],
      unitName: [
        { required: true, message: '请选择所属单位', trigger: 'change', },
      ],
      talentLevel: [
        { required: true, message: '请选择人才级别', trigger: 'change', },
      ],
      talentClass: [
        { required: true, message: '请选择人才类别', trigger: 'change', },
      ],
      title: [
        { required: true, message: '请选择获得称号', trigger: 'change', },
      ],
      confirmDate: [
        { required: true, message: '请选择认定时间', trigger: 'change' },
      ],
      awardUnit: [
        { required: true, message: '请输入认定单位', trigger: 'blur', },
      ],
      sex: [
        { required: true, message: '请选择性别', trigger: 'change' },
      ],
      birthdar_date: [
        { required: true, message: '请输入年龄', trigger: 'blur' },
      ],
    })

    const ruleFormRef = ref()
    const submitForm = async () => {
      await ruleFormRef.value.validate((valid, fields) => {
        if (valid) {
          emit('valueFlag', true)
          console.log('submit!')
        } else {
          emit('valueFlag', false)
        }
      })
    }

    const handleChangeDate = (val) => {
      const year = val.getFullYear(); // 获取年份
      const month = (val.getMonth() + 1) < 10 ? '0' + (val.getMonth() + 1) : (val.getMonth() + 1); // 获取月份，需要加1
      const day = val.getDate() < 10 ? '0' + val.getDate() : val.getDate(); // 获取日期
      const formattedDate = `${year}-${month}-${day}`;
      form.confirmDate = formattedDate
      store.commit('innovativeTalents.getTalentTeamInfo', { ...form })
    }


    return {
      labelPosition,
      data,
      form,
      rules,
      ruleFormRef,
      submitForm,
      handleChangeDate,
      deptForm,

      changeValue,
      changeValue1,
      changeValue2,
      changeValue3,
      changeValue4,
      changeValue5,
    }
  },
}
</script>
<style lang="scss" scoped>
.demo-form-inline .el-form-item {
  width: 48%;
}

:deep(.labelHeight .el-form-item__label) {
  line-height: 20px;
}

:deep(.el-date-editor.el-input) {
  width: 100%;
}

:deep(.el-date-editor.el-input__inner) {
  width: 100%;
}

:deep(.demo-form-inline .el-select) {
  width: 100%;
  margin: 0;
}

.el-select {
  width: 100%;
  margin: 0;
}

.selectLabel {
  width: 70%;
  min-height: 40px;
  line-height: 40px;
  padding: 0 15px;
  border: 1px solid #ccc;
  border-radius: 6px;
  display: inline-block;
}

.addLabel {
  display: inline-block;
  width: 120px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  background-color: #169bd5;
  border-radius: 6px;
  color: #fff;
  margin-left: 15px;
}

.unitBox {
  width: 100%;
  margin: 30px 0;

  .title {
    padding: 15px 0;
    border-bottom: 1px solid #ccc;
  }

  .tagBox {
    line-height: 40px;
    margin-top: 15px;
    border: 1px solid #ccc;
    border-radius: 8px;
    padding: 10px 15px;
  }
}

.el-tag {
  margin-right: 10px;
}
</style>
